package com.example.forum.services.impl;

import com.example.forum.dao.BoardMapper;
import com.example.forum.model.Board;
import com.example.forum.services.BoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements BoardService {
    @Resource
    private BoardMapper boardMapper;

    /**
     * 创建一个新板块————Session、Board(管理员功能)
     * @param board
     * @return
     */
    @Override
    public int create(Board board) {
        //还要设计一下默认值
        board.setArticleCount(1); //帖⼦数量
        board.setState((byte) 0); //状态
        board.setDeleteState((byte) 0); //是否删除
        Date date = new Date();
        board.setCreateTime(date); //创建时间
        board.setUpdateTime(date); //更新时间
        int row = boardMapper.insertSelective(board);
        return row;
    }

    /**
     * 获取Board对象————Session、板块id
     * @param id
     * @return
     */
    @Override
    public Board selectById(Long id) {
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    /**
     * 获取前n个正常的板块————Session、n
     * @param n
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer n) {
        return boardMapper.selectByNum(n);
    }

    /**
     * 获取所有正常的板块————Session
     * @return
     */
    @Override
    public List<Board> selectAllNormal() {
        return boardMapper.selectAllNormal();
    }

    /**
     * 板块的文章总数增加————Session、板块id
     * @param id
     * @return
     */
    @Override
    public int addOneArticleCountById(Long id) {
        Board board = boardMapper.selectByPrimaryKey(id);
        board.setArticleCount(board.getArticleCount()+1);
        boardMapper.updateByPrimaryKey(board);
        return 0;
    }

    /**
     * 板块的文章总数减少————Session、板块id
     * @param id
     * @return
     */
    @Override
    public int subOneArticleCountById(Long id) {
        Board board = boardMapper.selectByPrimaryKey(id);
        board.setArticleCount(board.getArticleCount()-1);
        boardMapper.updateByPrimaryKey(board);
        return 0;
    }

}
